package com.lz.fruitday.fragment;


import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.lz.fruitday.R;
import com.lz.fruitday.adapter.FriendAdapter;
import com.lz.fruitday.bean.Data;
import com.lz.fruitday.bean.FoodBaseBean;
import com.lz.fruitday.bean.FoodMainBean;
import com.lz.fruitday.bean.FriendMainBean;
import com.lz.fruitday.constant.Path;

import java.util.ArrayList;

import cz.msebera.android.httpclient.Header;

/**
 * A simple {@link Fragment} subclass.
 */
public class FriendsFragment extends Fragment {
    @ViewInject(R.id.ptr_food_friend)
    private PullToRefreshListView ptrFriends;
    private ListView lvFriend;
    private ArrayList<Data> datas=new ArrayList<>();
    private ArrayList<FriendMainBean> mainBeans=new ArrayList<>();
    private FriendAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       View view= inflater.inflate(R.layout.fragment_friends, container, false);
        ViewUtils.inject(this,view);
        initView();
        initData();
        return view;
    }

    private void initView() {
        ptrFriends.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        lvFriend = ptrFriends.getRefreshableView();
        adapter = new FriendAdapter(datas,getActivity());
        lvFriend.setAdapter(adapter);
    }

    private void initData() {
        AsyncHttpClient client=new AsyncHttpClient();
        client.post(Path.PATH_KIND, getLvParams(), new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                TypeReference<FoodBaseBean<FriendMainBean>> typeReference=new TypeReference<FoodBaseBean<FriendMainBean>>(){};
                FoodBaseBean baseBean= JSONObject.parseObject(responseString,typeReference);
                mainBeans.addAll(baseBean.getMain());
                for (int i = 0; i < mainBeans.size(); i++) {
                    FriendMainBean mainBean = mainBeans.get(i);
                    Data data=mainBean.getData();
                    datas.add(data);
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    private RequestParams getLvParams() {
        RequestParams params=new RequestParams();
        params.add("sign","b18baf872d1c1a80018e139dd5a93525");
        params.add("limit","20");
        params.add("timestamp","1438940967");
        params.add("platform","ANDROID");
        params.add("source","app");
        params.add("page","3");
        params.add("connect_id","");
        params.add("region_id","143949");
        params.add("service","fruit.getMaxArticleList");
        params.add("device_id","93789e8978f2eefdcf52006b4d96ee23");
        params.add("channel","qq");
        params.add("version","2.3.0");
        return params;
    }


}
